/****************************************************************************
 * Copyright (c) 2008, 2009 Andreas Unger and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Andreas Unger - initial API and implementation 
 ****************************************************************************/

package org.esmp.sysml.componentmodel.codegen;

import org.eclipse.emf.common.util.URI;
import org.eclipse.uml2.uml.UMLPlugin;
import org.eclipse.xtend.typesystem.emf.EcoreUtil2;
import org.eclipse.xtend.typesystem.emf.Mapping;
import org.esmp.sysml.componentmodel.profile.rtos.RTOSPackage;

/**
 * @author Andreas Unger
 *
 */
public class RTOSSetup extends SysMLSetup {

	private static final String COMPONENT_MODEL_PLUGIN_NAME = "org.esmp.sysml.componentmodel.profile";
	
	/**
	 * 
	 */
	public RTOSSetup() {
		addEPackageClass(RTOSPackage.eINSTANCE.getClass().getName());
	}
	
	public void setRtosSetup(boolean b) {
		super.setSysMLSetup(b);
		if (b) {
			registerRTOSProfile();
		}
	}
	
	private void registerRTOSProfile() {
		String p = "org/esmp/sysml/componentmodel/profile/rtos/RTOSPackage.class";
		URI uri = EcoreUtil2.getURI(p);
		if (uri != null && !uri.toString().equals(p)) {
			String path = uri.toString();
			final int mmIndex;
			if (path.endsWith("/bin/" + p)) {
				mmIndex = path.lastIndexOf("/bin/" + p);
			} else {
				mmIndex = path.lastIndexOf("/" + p);
			}
			if (mmIndex < 0) {
				throw new IllegalStateException(createClasspathErrorMessage(COMPONENT_MODEL_PLUGIN_NAME));
			}
			path = path.substring(0, mmIndex);
			path = addJarProtocolIfNecessary(path);
			addUriMap(new Mapping("pathmap://ESMP_ComponentModel_PROFILES/", path + "/profiles/"));
			
			UMLPlugin.getEPackageNsURIToProfileLocationMap().put("http://www.esmp.org/SysML/ComponentModel/RTOS", URI.createURI(path + "/profiles/RTOS.profile.uml#/"));
		} else {
			throw new IllegalStateException(createClasspathErrorMessage(COMPONENT_MODEL_PLUGIN_NAME));
		}
	}

}
